// OverLay.cpp : implementation file
//

#include "stdafx.h"
#include "RigCalib.h"
#include "OverLay.h"
#include "gui_size_macro.h"
#include "cvvImage.h"


// COverLay dialog

IMPLEMENT_DYNAMIC(COverLay, CDialog)

COverLay::COverLay(CWnd* pParent /*=NULL*/)
	: CDialog(COverLay::IDD, pParent)
{

}

COverLay::~COverLay()
{
}

void COverLay::updateImages(char *pdata, char *pRightData, int w, int h, int nChannels, 
							const vector<CvxMarker> &leftMarkers, const vector<CvxMarker> &rightMarkers, bool flipRight)
{
	//@todo add update flag
	assert(nChannels == 2);
	assert(pdata);
	assert(pRightData);

	//update image formate
	if (!m_leftImage)
	{
		m_leftImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	else if (m_leftImage->width != w || m_leftImage->height != h)
	{
		cvReleaseImage(&m_leftImage);
		m_leftImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}

	if (!m_rightImage)
	{
		m_rightImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	else if (m_rightImage->width != w || m_rightImage->height != h)
	{
		cvReleaseImage(&m_rightImage);
		m_rightImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}

	if (!m_anaglyphImage)
	{
		m_anaglyphImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	else if (m_anaglyphImage->width != w || m_anaglyphImage->height != h)
	{
		cvReleaseImage(&m_anaglyphImage);
		m_anaglyphImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}

//	m_anaglyphImage

	cvxYUV422toRGB(w, h, (unsigned char *)pdata, m_leftImage, false);
	if (pRightData)
	{
		cvxYUV422toRGB(w, h, (unsigned char *)pRightData, m_rightImage, flipRight);
	}

	InvalidateRect(0, FALSE);

}

void COverLay::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_OVERLAY_PIC, m_overlayPic);
}


BEGIN_MESSAGE_MAP(COverLay, CDialog)
	ON_WM_PAINT()
END_MESSAGE_MAP()


// COverLay message handlers

BOOL COverLay::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	int w = OVERLAY_PICTURE_WIDTH;
	int h = OVERLAY_PICTURE_HEIGHT;
	m_overlayPic.MoveWindow(20, 10, w, h);

	m_anaglyphImage = NULL;
	m_leftImage = NULL;
	m_rightImage = NULL;

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void COverLay::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages

	//r from left camera, g b from right camera
	if (m_leftImage && m_rightImage && m_anaglyphImage)
	{
		IplImage *fromImages[] = {m_leftImage, m_rightImage};

		int from_to[] = {2,2, 3,0, 4,1};
		cvMixChannels((const CvArr **)fromImages, 2, (CvArr**)&m_anaglyphImage, 1,from_to,  3);

		//draw anaglyph image
		CmfcImage anaglypyImageDrawer;		
		CDC *pDc = m_overlayPic.GetDC();	
		CRect rect;
		ASSERT(pDc);
		HDC hDc = pDc->GetSafeHdc();
		m_overlayPic.GetClientRect(&rect);
		anaglypyImageDrawer.passImage(m_anaglyphImage);
		anaglypyImageDrawer.DrawToHDC(hDc, &rect);
		ReleaseDC(pDc);
	}	
}
